package cetcbigdata.da.service.beijing.facilitation;

import cetcbigdata.da.base.DownloadAndUploadMinio;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description UtilBeiJing
 * @since 2022/9/16 14:52
 */


public class UtilBeiJing {
    public static List<JSONObject> getUrls(String html){
        List<JSONObject> out = new ArrayList<>();

        JXDocument jxDocument = new JXDocument(html);
        List<Object> rs = jxDocument.sel("//div[@class='centerMain']//a[@way]");
        for (Object o : rs){
            JSONObject outItem = new JSONObject();
            String name  = ((Element) o).attr("way");
            String url  = ((Element) o).attr("href");
            outItem.put("name", name);
            outItem.put("url", url );
            out.add(outItem);
        }
        return out;
    }

    public static String getJsonAddr(String url){
        // 编写正则表达式
        String regFileName = "/pubtask/json/(.+)\\.json";
        // 匹配当前正则表达式
        Matcher matcher = Pattern.compile(regFileName).matcher(url);
        // 定义当前文件的文件名称
        String fileName = "";
        // 判断是否可以找到匹配正则表达式的字符
        if (matcher.find()) {
            // 将匹配当前正则表达式的字符串即文件名称进行赋值
            fileName = matcher.group();
        }
        // 返回
        return fileName;
    }

    public static String fileDownloadAndUploadMinioGetImage(String url, String fileName, JSONObject headers,  JSONObject param)  {
        try {
            return DownloadAndUploadMinio.getInstance().downloadGet(
                    url,
                    headers,
                    param,
                    fileName
            );
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";

    }

    public static List<JSONObject> getRegionList(){
        String response = "{\"success\":true,\"code\":\"200\",\"data\":{\"locationName\":\"北京市\",\"orderNum\":0,\"id\":\"110000000000\",\"classification\":\"2\",\"delFlag\":\"0\",\"parentId\":\"1\",\"childNode\":[{\"locationName\":\"东城区\",\"orderNum\":1,\"id\":\"110101000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"西城区\",\"orderNum\":2,\"id\":\"110102000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"朝阳区\",\"orderNum\":3,\"id\":\"110105000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"丰台区\",\"orderNum\":4,\"id\":\"110106000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"石景山区\",\"orderNum\":5,\"id\":\"110107000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"海淀区\",\"orderNum\":6,\"id\":\"110108000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"门头沟区\",\"orderNum\":7,\"id\":\"110109000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"房山区\",\"orderNum\":8,\"id\":\"110111000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"副中心（通州区）\",\"orderNum\":9,\"id\":\"110112000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"顺义区\",\"orderNum\":10,\"id\":\"110113000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"昌平区\",\"orderNum\":11,\"id\":\"110114000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"大兴区\",\"orderNum\":12,\"id\":\"110115000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"怀柔区\",\"orderNum\":13,\"id\":\"110116000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"平谷区\",\"orderNum\":14,\"id\":\"110117000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"密云区\",\"orderNum\":15,\"id\":\"110118000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"延庆区\",\"orderNum\":16,\"id\":\"110119000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"经济开发区\",\"orderNum\":17,\"id\":\"110115403000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"}]}}";
        JSONArray data = JSONObject.parseObject(response).getJSONObject("data").getJSONArray("childNode");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : data){
            out.add((JSONObject) o);
        }
        return out;
    }

}
